package team.seekers.catering.food.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.catering.common.utils.DateUtil;
import team.seekers.catering.food.entity.dao.ComboComment;
import team.seekers.catering.user.entity.dao.User;
import team.seekers.catering.food.entity.vo.list.ComboCommentListVO;
import team.seekers.catering.food.mapper.ComboCommentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.catering.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户对套餐的评价 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2022-03-12
 */
@Service
public class ComboCommentService extends ServiceImpl<ComboCommentMapper, ComboComment>{

    @Autowired
    private UserService userService;

    public List<ComboCommentListVO> listByComboId(String comboId) {
        QueryWrapper<ComboComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_id",comboId);
        queryWrapper.orderByDesc("gmt_create");
        List<ComboComment> list = list(queryWrapper);

        List<ComboCommentListVO> comboCommentListVOS = new ArrayList<>();
        for (ComboComment comboComment : list) {
            ComboCommentListVO comboCommentListVO = new ComboCommentListVO();
            BeanUtils.copyProperties(comboComment,comboCommentListVO);
            User user = userService.getById(comboComment.getUserId());
            comboCommentListVO.setUsername(user.getName());
            comboCommentListVO.setUserAvatar(user.getAvatar());
            List<String> pictures = JSON.parseArray(comboComment.getPictures(), String.class);
            comboCommentListVO.setPictures(pictures);
            comboCommentListVO.setGmtCreate(DateUtil.formatDate(comboComment.getGmtCreate()));
            comboCommentListVOS.add(comboCommentListVO);
        }
        return comboCommentListVOS;
    }
}
